package com.richman.hgps38;

import java.util.Vector;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import com.richman.controller.BetClient;
import com.richman.ps38.entity.Event;
import com.richman.ps38.entity.HGEvent;
import com.richman.ps38.entity.HGPS38Event;
import com.richman.ps38.entity.SendData;

public class HGPS38EventManager {
	
	private  ReadWriteLock hgeventsLock = new ReentrantReadWriteLock();
	private  Vector<HGEvent>  hgevents= new Vector<HGEvent>();

	private  ReadWriteLock ps38eventsLock = new ReentrantReadWriteLock();
	private  Vector<Event> ps38events = new 	Vector<Event>();
	

	
	private  ReadWriteLock hgps38eventsLock = new ReentrantReadWriteLock();
	private  Vector<HGPS38Event> hgps38events = new Vector<HGPS38Event>(); 
	
	
	
	private  ReadWriteLock senddataLock = new ReentrantReadWriteLock(); 
	private  Vector<SendData> senddatas = new Vector<SendData>();
	
	
	
	
	
	public void updatehgevents(Vector<HGEvent> hg) {
		
		
		try {
			
			hgeventsLock.writeLock().lock();
			
			hgevents.clear();
			
			for(int i = 0; i < hg.size(); i++) {
				hgevents.add(hg.elementAt(i));
			}
			
			
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}

		hgeventsLock.writeLock().unlock();
		
	}
	
	public Vector<HGEvent> gethgvents() {
		
		try {
			
			hgeventsLock.readLock().lock();
			
			Vector<HGEvent> hgeventsTmp = new Vector<HGEvent>();
			
			for(int i = 0; i < hgevents.size(); i++) {
				hgeventsTmp.add(hgevents.elementAt(i));
			}
			
			hgeventsLock.readLock().unlock();
			
			return hgeventsTmp;
			
		}catch(Exception e) {
			e.printStackTrace();
			
			hgeventsLock.readLock().unlock();
			
		}
		
		return null;
		
		
		
	}
	
	
	public void updateps38events(Vector<Event> ps38) {
		
		
		try {
			
			ps38eventsLock.writeLock().lock();
			
			ps38events.clear();
			
			for(int i = 0; i < ps38.size(); i++) {
				ps38events.add(ps38.elementAt(i));
			}
			
			
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}

		ps38eventsLock.writeLock().unlock();
		
	}
	
	public Vector<Event> getps38vents() {
		
		try {
			
			ps38eventsLock.readLock().lock();
			
			Vector<Event> ps38eventsTmp = new Vector<Event>();
			
			for(int i = 0; i < ps38events.size(); i++) {
				ps38eventsTmp.add(ps38events.elementAt(i));
			}
			
			ps38eventsLock.readLock().unlock();
			
			return ps38eventsTmp;
			
		}catch(Exception e) {
			e.printStackTrace();
			
			ps38eventsLock.readLock().unlock();
			
		}
		
		return null;
		
		
		
	}
	
	
	
	
	
	
	public void constructhgps38events() {
		
		try {
			hgps38eventsLock.writeLock().lock();
			
			hgeventsLock.readLock().lock();
			ps38eventsLock.readLock().lock();
			senddataLock.writeLock().lock();
			
			
			hgps38events.clear();
			
			Vector<String[]> hgps38teams = BetClient.mainWnd.getHGPS38TeamsList();
			
			
			
			for(int i = 0; i < hgevents.size(); i++) {
				
				String ps38hometeam = "";
				String ps38awayteam = "";
				
				HGEvent hgeventtmp = hgevents.elementAt(i);
				
				for(int j = 0; j < hgps38teams.size(); j++) {
					if(hgps38teams.elementAt(j)[0].equals(hgeventtmp.homeTeam)) {
						ps38hometeam = hgps38teams.elementAt(j)[1];
						break;
					}
				}
				
				if(!ps38hometeam.equals("")) {
					
					for(int j = 0; j < hgps38teams.size(); j++) {
						if(hgps38teams.elementAt(j)[0].equals(hgeventtmp.awayTeam)) {
							ps38awayteam = hgps38teams.elementAt(j)[1];
							break;
						}
					}
					
				}
				
				
				if(!ps38hometeam.equals("") && !ps38awayteam.equals("")) {
					for(int j = 0; j < ps38events.size(); j++) {
						
						if((ps38events.elementAt(j).homeTeam + "-vs-" + ps38events.elementAt(j).awayTeam).equals(ps38hometeam + "-vs-" + ps38awayteam)) {
							
							HGPS38Event hgps38eventtmp = new HGPS38Event();
							//比较盘口，大小盘
							
							boolean add = false;
							
							Event ps38eventtmp = ps38events.elementAt(j);
							
							
							//全场
							//if(hgeventtmp.wholerqppankou.equals(ps38eventtmp.wholerqppankou)) {
							if(!hgeventtmp.wholerqppankou.equals("") && !ps38eventtmp.wholerqppankou.equals("") && ( Double.parseDouble(hgeventtmp.wholerqppankou) -  Double.parseDouble(ps38eventtmp.wholerqppankou) < 0.001 )) {
								
								hgps38eventtmp.wholerqppankou = hgeventtmp.wholerqppankou;
								
								//hg主赔 + ps38客赔
								
								if(!hgeventtmp.wholeRQPPankouUp.equals("") && !ps38eventtmp.wholeRQPPankouDown.equals("")) {
									//hgps38eventtmp.wholeRQPPankouUp = Double.toString(Double.parseDouble(hgeventtmp.wholeRQPPankouUp) + Double.parseDouble(ps38eventtmp.wholeRQPPankouDown));
									hgps38eventtmp.wholeRQPPankouUp = String.format("%.3f",Double.parseDouble(hgeventtmp.wholeRQPPankouUp) + Double.parseDouble(ps38eventtmp.wholeRQPPankouDown));
									add = true;
								}
								

								//hg客队 +ps38主赔
								if(!hgeventtmp.wholeRQPPankouDown.equals("") && !ps38eventtmp.wholeRQPPankouUp.equals("")) {
									hgps38eventtmp.wholeRQPPankouDown = String.format("%.3f",Double.parseDouble(hgeventtmp.wholeRQPPankouDown) + Double.parseDouble(ps38eventtmp.wholeRQPPankouUp));
									add = true;
								}

							}
							
							
							//if(hgeventtmp.wholeDXPPankou.equals(ps38eventtmp.wholeDXPPankou)) {
							if(!hgeventtmp.wholeDXPPankou.equals("") && !ps38eventtmp.wholeDXPPankou.equals("") && ( Double.parseDouble(hgeventtmp.wholeDXPPankou) -  Double.parseDouble(ps38eventtmp.wholeDXPPankou) < 0.001 )) {	
								
								//hg大赔 + ps38小赔
								
								hgps38eventtmp.wholeDXPPankou = hgeventtmp.wholeDXPPankou;
								
								if(!hgeventtmp.wholeDXPPankouUp.equals("") && !ps38eventtmp.wholeDXPPankouDown.equals("")) {
									hgps38eventtmp.wholeDXPPankouUp = String.format("%.3f",Double.parseDouble(hgeventtmp.wholeDXPPankouUp) + Double.parseDouble(ps38eventtmp.wholeDXPPankouDown));
									add = true;
								}
								
								
								//hg小赔 +ps38大赔
								
								if(!hgeventtmp.wholeDXPPankouDown.equals("") && !ps38eventtmp.wholeDXPPankouUp.equals("")) {
									hgps38eventtmp.wholeDXPPankouDown = String.format("%.3f",Double.parseDouble(hgeventtmp.wholeDXPPankouDown) + Double.parseDouble(ps38eventtmp.wholeDXPPankouUp));
									
									add = true;
								}

								
							}
							
							
							//半场
							
							//if(hgeventtmp.halfrqppankou.equals(ps38eventtmp.halfrqppankou)) {
							if(!hgeventtmp.halfrqppankou.equals("") && !ps38eventtmp.halfrqppankou.equals("") && ( Double.parseDouble(hgeventtmp.halfrqppankou) -  Double.parseDouble(ps38eventtmp.halfrqppankou) < 0.001 )) {	
								
								//hg主赔 + ps38客赔
								
								hgps38eventtmp.halfrqppankou = hgeventtmp.halfrqppankou;
								
								if(!hgeventtmp.halfRQPPankouUp.equals("") && !ps38eventtmp.halfRQPPankouDown.equals("")) {
									hgps38eventtmp.halfRQPPankouUp = String.format("%.3f",Double.parseDouble(hgeventtmp.halfRQPPankouUp) + Double.parseDouble(ps38eventtmp.halfRQPPankouDown));
									add = true;
								}
								

								//hg客队 +ps38主赔
								if(!hgeventtmp.halfRQPPankouDown.equals("") && !ps38eventtmp.halfRQPPankouUp.equals("")) {
									hgps38eventtmp.halfRQPPankouDown = String.format("%.3f",Double.parseDouble(hgeventtmp.halfRQPPankouDown) + Double.parseDouble(ps38eventtmp.halfRQPPankouUp));
									add = true;
								}

							}
							
							
							//if(hgeventtmp.halfDXPPankou.equals(ps38eventtmp.halfDXPPankou)) {
							if(!hgeventtmp.halfDXPPankou.equals("") && !ps38eventtmp.halfDXPPankou.equals("") && ( Double.parseDouble(hgeventtmp.halfDXPPankou) -  Double.parseDouble(ps38eventtmp.halfDXPPankou) < 0.001 )) {	
								
								//hg大赔 + ps38小赔
								hgps38eventtmp.halfDXPPankou = hgeventtmp.halfDXPPankou;
								
								if(!hgeventtmp.halfDXPPankouUp.equals("") && !ps38eventtmp.halfDXPPankouDown.equals("")) {
									hgps38eventtmp.halfDXPPankouUp = String.format("%.3f",Double.parseDouble(hgeventtmp.halfDXPPankouUp) + Double.parseDouble(ps38eventtmp.halfDXPPankouDown));
									add = true;
								}
								
								
								//hg小赔 +ps38大赔
								
								if(!hgeventtmp.halfDXPPankouDown.equals("") && !ps38eventtmp.halfDXPPankouUp.equals("")) {
									hgps38eventtmp.halfDXPPankouDown = String.format("%.3f",Double.parseDouble(hgeventtmp.halfDXPPankouDown) + Double.parseDouble(ps38eventtmp.halfDXPPankouUp));
									
									add = true;
								}

								
							}
							
							
							
							if(add) {
								
								hgps38eventtmp.leagueName = hgeventtmp.leagueName;
								hgps38eventtmp.homeTeam = hgeventtmp.homeTeam;
								hgps38eventtmp.awayTeam = hgeventtmp.awayTeam;
								hgps38eventtmp.eventTime = hgeventtmp.eventTime;
								
								
								hgps38events.add(hgps38eventtmp);
								
								SendData senddata = new SendData();
								senddata.hgevent = hgeventtmp;
								senddata.event = ps38eventtmp;
								
								senddatas.add(senddata);
								
							}
							
							
							
							break;
						}
						
					}
				}
				
				
			}
			
			
			
			BetClient.mainWnd.updateHGPS38EventsTable(hgps38events);

				
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		hgps38eventsLock.writeLock().unlock();
		hgeventsLock.readLock().unlock();
		ps38eventsLock.readLock().unlock();
		
		senddataLock.writeLock().unlock();
		
	}
	
	
	
	
	
	public Vector<SendData> getsenddatas() {
		
		
		Vector<SendData> vec = new Vector<SendData>();
		
		senddataLock.readLock().lock();
		
		
		
		for(int i = 0; i < senddatas.size(); i++){
		
			
			
			
			vec.add(senddatas.elementAt(i));
			

			
		}
		
		senddataLock.readLock().unlock();
		
		return vec;
		
		
		
	}
	
	
	
	
	
}
